<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Gregwar\Captcha\CaptchaBuilder;

class LoginController extends Controller
{
	//加载登录模块
    public function login()
    {
        return view('admin.login');
    }
	//执行用户登录
	public function doLogin(Request	$request)
	{
		//执行验证码判断
		$mycode = $request->input("mycode");
		$yanzhengma = \Session::get('phrase');
		if($mycode !== $yanzhengma){
			//回滚
			return back()->with("msg","验证码错误！！");
		}
		//执行登录判断
		$uname = $request->input("uname");
		$pwd = $request->input("pwd");
		//获取对应用户账号信息
		$user = \DB::table("u_user")->where("uname",$uname)->first();
		if(!empty($user)){
			//判断密码
			if(md5($pwd) == $user->pwd){
				//存储用户账号表中的用户名到session跳转页面
				session()->set("adminuser",$user);
				//获取对应用户个人信息
				$pic = \DB::table("u_info")->where("uid",$user->id)->first();
				//存储用户个人信息表中的头像到session跳转页面
				session()->set("adminuser",$pic->hpic);
				return redirect("admin");
			}
		}
		return back()->with("msg","账号或密码错误！！");
	}
	//加载验证码
	public function getCode()
	{
		$builder = new CaptchaBuilder;
        $builder->build(150,32);
        \Session::set('phrase',$builder->getPhrase()); //存储验证码
        return response($builder->output())->header('Content-type','image/jpeg');
	}
	//执行退出	
	public function logout(Request $request)
   {
       $request->session()->flush();
       return redirect("admin/login");
   }
}
